//
//  TZYDebrisViewController.m
//  Moment
//
//  Created by dllo on 15/12/26.
//  Copyright © 2015年 片刻. All rights reserved.
//

#import "TZYDebrisViewController.h"
#import "TZYDebrisList.h"
#import "TZYDebrisListViewController.h"

@interface TZYDebrisViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain)UICollectionView *myCollectionView;
@property (nonatomic, assign)BOOL isUpLoad;
@property (nonatomic, retain)NSMutableArray *fragmentListArray;


@end

@implementation TZYDebrisViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    
     self.fragmentListArray = [NSMutableArray array];
    
    
    
    [self createColletionView];
    
    [self addHeaderRefresh];
    
    
    UIImage *image = [UIImage imageNamed:@"back.png"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction:)];
    
}


- (void)leftButtonAction:(UIBarButtonItem *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark -- 设置下拉刷新
-(void)addHeaderRefresh
{
    //当下拉时候会触发该方法
    self.myCollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        NSLog(@"下拉");
        
        //self.nextPage = 0;// 记录页码的
        self.isUpLoad = NO;//标记为下拉操作
        // 重新请求数据
        [self getAFNetWorkingData];
        
    }];
    
    // 进到应用自动刷新(可以放在viewdidload)
    [self.myCollectionView.mj_header beginRefreshing];
}



- (void)getAFNetWorkingData
{
    NSString *urlStr = @"http://api2.pianke.me/timeline/tags";
    NSString *para = [NSString stringWithFormat:@"auth=&client=1&deviceid=DC7311B8-4DFE-4BC2-99D9-565BF1215670&limit=10&start=0&version=3.0.6"];
    
    [GetData postUrl:urlStr body:para result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        if (self.isUpLoad == NO) {
            //说明是下拉，就要清空数组中的数据
            [self.fragmentListArray removeAllObjects];
        }
        NSMutableDictionary *bigDic = [result objectForKey:@"data"];
        NSMutableArray *array = [bigDic objectForKey:@"list"];
        for (NSMutableDictionary *dic in array) {
            TZYDebrisList *debrisList = [[TZYDebrisList alloc] init];
            [debrisList setValuesForKeysWithDictionary:dic];
            [self.fragmentListArray addObject:debrisList];
            
        }
        
        [self.myCollectionView.mj_header endRefreshing];
        [self.myCollectionView.mj_footer endRefreshing];
        [self.myCollectionView reloadData];
        
    } failure:^(NSError *error) {
        
        
    }];
}






- (void)createColletionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 设置每个item(cell)的大小
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width / 3.2, self.view.frame.size.width / 3.2);
    // 设置最小行间距
    flowLayout.minimumLineSpacing = 2;
    // 设置最小列间距
    flowLayout.minimumInteritemSpacing = 2;
    // 设置item与四周边界的距离  上左下右
    flowLayout.sectionInset = UIEdgeInsetsMake(self.view.frame.size.height / 67, self.view.frame.size.width / 42.6, self.view.frame.size.height / 67, self.view.frame.size.width / 42.6);
    // 设置滚动方向
    //    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:flowLayout];
    //self.myCollectionView = [[UICollectionView alloc] init];
    self.myCollectionView.collectionViewLayout = flowLayout;
    [self.view addSubview:self.myCollectionView];
    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    
    
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    self.myCollectionView.showsVerticalScrollIndicator = NO;
    
    //[self.myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.myCollectionView registerClass:[TZYDebrisCollectionViewCell class] forCellWithReuseIdentifier:@"TZYDebrisCollectionViewCell"];
    
}


#pragma mark -- 设置显示Item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.fragmentListArray.count;
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"TZYDebrisCollectionViewCell";
    
    TZYDebrisCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    
    //fragmentCell.fragList = [self.fragmentListArray objectAtIndex:indexPath.item];
    
//    cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
    
    cell.debrisList = [self.fragmentListArray objectAtIndex:indexPath.item];
    
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    TZYDebrisListViewController *debrisListVC = [[TZYDebrisListViewController alloc] init];
    
    TZYDebrisList *debrisList = [self.fragmentListArray objectAtIndex:indexPath.item];
    debrisListVC.nameStr = debrisList.tag;
    
    [self.navigationController pushViewController:debrisListVC animated:YES];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
